package com.eden.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eden.common.ErrorCode;
import com.eden.common.R;
import com.eden.dto.LoginUserDto;
import com.eden.dto.UserDto;
import com.eden.entity.SysUser;
import com.eden.mapper.SysUserMapper;
import com.eden.service.LoginService;
import com.eden.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private LoginService loginService;

    @Override
    public SysUser findUserById(Long id) {
        //根据id查询
        //为防止sysUser为空增加一个判断
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("伊甸园");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        //account id 头像 名称
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAdmin,SysUser::getNickname);
        //增加查询效率，只查询一条
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public R findUserByToken(String token) {

        /*
         * 1.token合法性校验
         *   是否为空  解析是否成功  redis是否存在
         * 2.如果校验失败  返回错误
         * 3.如果成功，返回对应的结果  LoginUserDto
         * */

        SysUser sysUser = loginService.checkToken(token);
        if (sysUser == null) {
            R.fail(ErrorCode.TOKEN_ERR0R.getCode(), ErrorCode.TOKEN_ERR0R.getMsg());
        }
            LoginUserDto loginUserDto = new LoginUserDto();
            loginUserDto.setId(sysUser.getId());
            loginUserDto.setNickname(sysUser.getNickname());
            loginUserDto.setAvatar(sysUser.getAvatar());
            loginUserDto.setAccount(sysUser.getAccount());
            return R.success(loginUserDto);

    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        //确保只能查询一条
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }


    @Override
    public UserDto findUserDtoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("Eden");
        }
        UserDto userDto = new UserDto();
        userDto.setAvatar(sysUser.getAvatar());
        userDto.setNickname(sysUser.getNickname());
        userDto.setId(sysUser.getId());
        return userDto;
    }

}
